package kuaidi.view.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import kuaidi.api.sqlite.RecordsDao;
import kuaidi.helper.R;
import kuaidi.view.activity.CompanyActivity;
import kuaidi.view.activity.LoginActivity;
import kuaidi.view.activity.SaomaActivity;


public class ChakuaidiFragment extends Fragment {


    @BindView(R.id.chakuaidi_tiaozhuan)
    ImageView chakuaidiTiaozhuan;

    public ChakuaidiFragment() {
        // Required empty public constructor
    }

    public static ChakuaidiFragment newInstance() {

        Bundle args = new Bundle();

        ChakuaidiFragment fragment = new ChakuaidiFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @BindView(R.id.chakuaidi_img1_iv)
    ImageView chakuaidiImg1Iv;
    @BindView(R.id.chakuaidi_danhao_et)
    EditText chakuaidiDanhaoEt;
    @BindView(R.id.chakuaidi_saomiao_iv)
    ImageView chakuaidiSaomiaoIv;
    @BindView(R.id.chakuaidi_img2_iv)
    ImageView chakuaidiImg2Iv;
    @BindView(R.id.chakuaidi_gongsi_et)
    EditText chakuaidiGongsiEt;
    @BindView(R.id.chakuaidi_xuanze_iv)
    ImageView chakuaidiXuanzeIv;
    @BindView(R.id.chakuaidi_chaxun_btn)
    Button chakuaidiChaxunBtn;
    private String no;
    private String com;
    private ArrayList<String> data = new ArrayList<>();
    private RecordsDao recordsDao;
    private List<String> tempList;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_chakuaidi, container, false);
        ButterKnife.bind(this, view);
        return view;
    }

    public String getCom() {
        String icom = null;
        if (com.equals("申通快递")) {
            icom = "sto";
        } else if (com.equals("圆通快递")) {
            icom = "yt";
        } else if (com.equals("韵达快递")) {
            icom = "yd";
        } else if (com.equals("天天快递")) {
            icom = "tt";
        } else if (com.equals("EMS快递")) {
            icom = "ems";
        } else if (com.equals("中通快递")) {
            icom = "zto";
        } else if (com.equals("汇通快递")) {
            icom = "ht";
        }
        return icom;
    }


    public String judegeNo() {
//        天天单号由14位数字组成，常见以6**、5*、00*等开头
//        EMS单号由13位字母和数字组成，开头和结尾二位是字母，中间是数字
//        汇通快递查询单号由13位数字编码组成，常见以0*或者B*、H*开头
//        韵达单号由13位数字组成，常见以10*、12*、19*等开头
//        速尔的快递单号由12位数字组成的
//        申通单号由12位数字组成，常见以268**、368**、58**等开头
//        中通单号由12位数字组成，常见以2008**、6**、010等开头
//        顺丰单号由12位数字组成，常见以电话区号后三位开头
//        宅急送单号由10位数字组成，常见以7**、6**、5**等开
//        圆通单号由10位字母数字组成，常见以1*、2*、6*、8*、D*及V*等开头
//        德邦的货运单号现在是以1或2开头的8位数字组成
        String icom = null;
        if (no.length() == 10) {
            icom = "圆通快递";
        } else if (no.length() == 12) {
            if (no.substring(0, 3).equals("268")
                    || no.substring(0, 3).equals("368")
                    || no.substring(0, 2).equals("58")) {
                icom = "申通快递";
            } else if (no.substring(0, 4).equals("2008")
                    || no.substring(0, 1).equals("6")
                    || no.substring(0, 3).equals("010")) {
                icom = "中通快递";
            }
        } else if (no.length() == 13) {
            if (no.substring(0).matches("[a-zA-Z]*")
                    || no.substring(no.length() - 1).matches("[a-zA-Z]*")) {
                icom = "EMS快递";
            } else if (no.substring(0, 2).equals("10")
                    || no.substring(0, 2).equals("12")
                    || no.substring(0, 2).equals("19")) {
                icom = "韵达快递";
            } else if (no.substring(0, 1).equals("0")
                    || no.substring(0, 1).equals("B")
                    || no.substring(0, 1).equals("H")) {
                icom = "汇通快递";
            }
        } else if (no.length() == 14) {
            icom = "天天快递";
        }
        return icom;
    }

    @OnClick({R.id.chakuaidi_saomiao_iv, R.id.chakuaidi_xuanze_iv, R.id.chakuaidi_chaxun_btn})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.chakuaidi_saomiao_iv:
                IntentIntegrator.forSupportFragment(this).initiateScan();

                break;
            case R.id.chakuaidi_xuanze_iv:

                Intent intent2 = new Intent(getActivity(), CompanyActivity.class);
                startActivityForResult(intent2, 1);
                break;
            case R.id.chakuaidi_chaxun_btn:
                Intent intent = new Intent(getActivity(), SaomaActivity.class);
                data.add(no);
                saveno(no);
                data.add(getCom());
                intent.putStringArrayListExtra("info", data);
                startActivity(intent);
                break;
        }
    }

    private void saveno(String no) {
        recordsDao = new RecordsDao(getContext());
        //判断数据库中是否存在该记录
        if (!recordsDao.isHasRecord(no)) {
            tempList.add(no);
        }
        //将搜索记录保存至数据库中
        recordsDao.addRecords(no);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
        if (result != null) {
            if (result.getContents() == null) {
                Toast.makeText(getContext(), "错误条形码", Toast.LENGTH_LONG).show();
            } else {
                if (result.getContents().toString().length() < 16) {
                    Toast.makeText(getContext(), "运单号: " + result.getContents(), Toast.LENGTH_LONG).show();
                    chakuaidiDanhaoEt.setText(result.getContents());
                    no = result.getContents();
                    chakuaidiGongsiEt.setText(com = judegeNo());
                } else {
                    Toast.makeText(getContext(), "你傻啊!这根本不是运单号!", Toast.LENGTH_LONG).show();
                }
            }
        } else {
            super.onActivityResult(requestCode, resultCode, data);
        }

        if (requestCode == 1 && resultCode == 2) {
            String data1 = data.getStringExtra("data");
            chakuaidiGongsiEt.setText(data1);
            com = data1;
        }
    }

    @OnClick(R.id.chakuaidi_tiaozhuan)
    public void onClick() {
        Intent intent=new Intent(getActivity(), LoginActivity.class);
        startActivity(intent);
    }
}
